﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skynet.LiteFoundation.BusinessEntities;
using Microsoft.Practices.Unity;
using System.ComponentModel;
using System.Data;
using Skynet.LiteFoundation.BusinessEntities;
using System.Data.Objects;
using System.Diagnostics;

namespace Skynet.LiteForm.Administration
{
    [System.ComponentModel.DataObject]
    public partial class MaintenanceExamCategory : System.Web.UI.Page
    {
        [Dependency]
        public SkyNetLibraryEntities DataContext { get; set; }

        protected void Page_Init(object sender, EventArgs e)
        {            
            this.odsExamCategory.ObjectCreating += delegate(object ocds, ObjectDataSourceEventArgs args) { args.ObjectInstance = this; };
            this.ocdsWorkExamCategory.ObjectCreating += delegate(object ocds, ObjectDataSourceEventArgs args) { args.ObjectInstance = this; };
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Page_Unload(object sender, EventArgs e)
        {
            if (this.DataContext != null)
                this.DataContext.Dispose();
        }

        

        [DataObjectMethod(DataObjectMethodType.Select)]
        public IEnumerable<ExamCategory> GetExamCategories()
        {
            IEnumerable<ExamCategory> result = null;  
            
                ObjectQuery<ExamCategory> cats = DataContext.ExamCategories;
                cats.MergeOption = MergeOption.NoTracking;
                var query = (from c in cats select c);

                var target = query.ToList();
                foreach (var item in target)
                {


                }
                return target; 
            
            
            
                
            
            return result;
            
            //Func<SkyNetLibraryEntities, ObjectQuery<ExamCategory>> function;
            //if (Application["Query"] == null)
            //{
            //    function = CompiledQuery.Compile<SkyNetLibraryEntities, ObjectQuery<ExamCategory>>(c => c.ExamCategories);
            //    Application["Query"] = function;
            //}
            //else
            //    function = Application["Query"] as Func<SkyNetLibraryEntities, ObjectQuery<ExamCategory>>;
            
            
            //return function.Invoke(this.DataContext).ToList();
        }


        [DataObjectMethod(DataObjectMethodType.Select)]
        public ExamCategory GetExamCategory(Guid Id)
        {
            this.DataContext.ContextOptions.LazyLoadingEnabled = false;
            var query = (from c in DataContext.ExamCategories 
                         where c.Id == Id 
                         select c).FirstOrDefault();
            return query;
        }


        [DataObjectMethod(DataObjectMethodType.Insert)]
        public void AddExamCategory(ExamCategory item)
        {
            item.Id = Guid.NewGuid(); 
            this.DataContext.ExamCategories.AddObject(item);
            this.DataContext.SaveChanges(); 
        }

        [DataObjectMethod(DataObjectMethodType.Update)]
        public void UpdateExamCategory(ExamCategory item)
        {
            EntityKey key = this.DataContext.CreateEntityKey(this.DataContext.ExamCategories.EntitySet.Name, item);
            object result = null;
            if (this.DataContext.TryGetObjectByKey(key, out result))
                this.DataContext.ApplyCurrentValues(key.EntitySetName, item);            
            this.DataContext.SaveChanges();
        }
        [DataObjectMethod(DataObjectMethodType.Delete)]
        public void Delete(ExamCategory item)
        {
            EntityKey key = this.DataContext.CreateEntityKey(this.DataContext.ExamCategories.EntitySet.Name, item);
            object result = null;
            if (this.DataContext.TryGetObjectByKey(key, out result))
                this.DataContext.DeleteObject(result);
            this.DataContext.SaveChanges();
        }
        protected void gvExamCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.dvExamCategory.ChangeMode(DetailsViewMode.Edit); 
        }
    }
}